package view.component.monster
{
	import embeds.EmbedRes;
	
	import enums.main.UISize;
	import enums.monster.MonsterNavigatorScreenName;
	
	import feathers.controls.Button;
	import feathers.controls.Screen;
	import feathers.controls.ScreenNavigator;
	import feathers.controls.ScreenNavigatorItem;
	import feathers.controls.TabBar;
	import feathers.data.ListCollection;
	import feathers.motion.transitions.ScreenFadeTransitionManager;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import util.AtlasTextureGenerator;
	import util.FG2FUGenerator;
	
	import view.component.IAnimateNaviView;
	import view.component.monster.large_monster.LargeMonsterView;
	import view.component.monster.small_monster.SmallMonsterView;
	
	
	
	
	/**
	 * 怪物情报页
	 * @author £iang
	 * 
	 */	
	public class MonsterView extends Screen implements IAnimateNaviView
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 大小型怪物切换 */
		public var mainTabBar:TabBar;
		/** 大小型怪物导航界面 */
		public var mainNavigator:ScreenNavigator;
		/** 返回按钮 */
		public var returnBtn:Button;
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 转场管理 */
		private var _transitManager:ScreenFadeTransitionManager;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			var iconVec:Vector.<Texture> = AtlasTextureGenerator.getRandomCatTextures(
				"head", 2 );
			var tmpImage:Image;
			var labelVec:Vector.<String> = new Vector.<String>();
			var dataProvider:ListCollection = new ListCollection( [] );
			
			labelVec.push( "大型怪物", "小型怪物" );
			mainTabBar = new TabBar();
			mainTabBar.gap = 30 * this.dpiScale;
			
			for ( var i:int = 0; i < 2; i ++ )
			{
				var obj:Object = new Object();
				obj.label = labelVec[ i ];
				tmpImage = new Image( iconVec[ i ] );
				tmpImage.scaleX = tmpImage.scaleY = this.dpiScale;
				obj.defaultSelectedIcon = tmpImage;
				dataProvider.push( obj );
			}
			
			mainTabBar.dataProvider = dataProvider;
			mainTabBar.tabFactory = mainTabFactory;
			this.addChild( mainTabBar );
			
			this.mainNavigator = new ScreenNavigator();
			this.mainNavigator.alpha = 0;
			this.addChild( mainNavigator );
			
			this.mainNavigator.addScreen(
				MonsterNavigatorScreenName.LARGE_MONSTER_SCREEN,
				new ScreenNavigatorItem( LargeMonsterView )
			);
			
			this.mainNavigator.addScreen(
				MonsterNavigatorScreenName.SMALL_MONSTER_SCREEN,
				new ScreenNavigatorItem( SmallMonsterView )
			);
			
			this._transitManager = new ScreenFadeTransitionManager( mainNavigator );
			_transitManager.duration = 0.4;
			
			var tmpTexture:Texture = Texture.fromBitmapData( new EmbedRes.RETURN_ICON().bitmapData );
			tmpImage = new Image( tmpTexture );
			
			tmpImage.scaleX = tmpImage.scaleY = this.dpiScale;
			returnBtn = FG2FUGenerator.generateRoundRectButton(
				70, UISize.TAB_BUTTON_H, this.dpiScale, 12, "small"
			);
			returnBtn.defaultIcon = tmpImage;
			this.addChild( returnBtn );
			returnBtn.alpha = 0;
			
			mainNavigator.showScreen( MonsterNavigatorScreenName.LARGE_MONSTER_SCREEN );
			
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			mainTabBar.x = - mainTabBar.width;
			mainTabBar.y = 20 * this.dpiScale;
			mainNavigator.y = UISize.MONSTER_TAB_AREA_H * this.dpiScale;
			returnBtn.x = UISize.MAIN_W - 90 * this.dpiScale;
			returnBtn.y = 20 * this.dpiScale;
		}
		
		/**
		 * 批量设置mainTabBar按钮 
		 * @return tabbar按钮
		 * 
		 */		
		private function mainTabFactory():Button
		{
			var button:Button = FG2FUGenerator.generateRoundRectButton(
				UISize.TAB_BUTTON_W, UISize.TAB_BUTTON_H,
				this.dpiScale, UISize.TAB_BUTTON_H * 0.9, "small"
			);
			
			return button;
		}
		
		/**
		 * 播放显示动画
		 * 
		 */	
		public function playShowAnimate():void
		{
			var tween:Tween = new Tween( mainTabBar, 0.4, "easeOut" );
			
			tween.animate( "x", 20 * this.dpiScale );
			Starling.juggler.add( tween );
			tween = new Tween( mainNavigator, 0.4 );
			tween.animate( "alpha", 1 );
			Starling.juggler.add( tween );
			
			tween = new Tween( returnBtn, 0.4);
			tween.animate( "alpha", 1 );
			Starling.juggler.add( tween );
		}
		
		/**
		 * 播放隐藏动画 
		 * 
		 */	
		public function playHideAnimate():void
		{
			var tween:Tween = new Tween( this, 0.4 );
			
			tween.animate( "alpha", 0 );
			Starling.juggler.add( tween );
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		public function addListener():void
		{
			mainTabBar.addEventListener( Event.CHANGE, tabChangeHandler );
			returnBtn.addEventListener( Event.TRIGGERED, returnTriggeredHandler );
		}
		
		/**
		 * 清除导航栏 
		 * 
		 */		
		public function clearNavi():void
		{
			_transitManager.delay = _transitManager.duration = 0;
			_transitManager.skipNextTransition = true;
			mainNavigator.removeAllScreens();
		}
		
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理tabBar切换时派发的事件 
		 * @param event tabBar切换时派发的事件 
		 *  
		 */		
		protected function tabChangeHandler( event:Event ):void
		{
			var index:int = mainTabBar.selectedIndex;
			
			switch(index)
			{
				case 0:
				{
					mainNavigator.showScreen( MonsterNavigatorScreenName.LARGE_MONSTER_SCREEN );
					break;
				}
					
				case 1:
				{
					mainNavigator.showScreen( MonsterNavigatorScreenName.SMALL_MONSTER_SCREEN );
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
		
		/**
		 * 处理点击返回按钮后派发的事件 
		 * @param event 点击返回按钮后派发的事件 
		 *  
		 */		
		protected function returnTriggeredHandler( event:Event ):void
		{
			dispatchEventWith( "back" );
		}
		
	
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.juggler.removeTweens( mainTabBar );
			Starling.juggler.removeTweens( mainNavigator );
			Starling.juggler.removeTweens( returnBtn );
			Starling.juggler.removeTweens( this );
			
			var button:Button;
			var k:int;
			
			for ( k = 0; k < mainTabBar.numChildren; k ++ )
			{
				if ( mainTabBar.getChildAt( k ) is Button )
				{
					button = mainTabBar.getChildAt( k ) as Button;
					button.dispose();
					FG2FUGenerator.deepDisposeButtonComp( button );
					button = null;
				}
			}
		
			returnBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( returnBtn );
			returnBtn = null;
			
			mainTabBar.dispose();
			mainTabBar = null;
			mainNavigator.dispose();
			mainNavigator = null;
			_transitManager = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function MonsterView()
		{
			super();
		}
	}
}